package cn.iocoder.yudao.module.member.assets.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.member.assets.dal.dataobject.CryptocurrencyIncreasingTaskRecordDO;
import jakarta.annotation.Nonnull;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.List;

/**
 * 联盟豆增值任务记录 Mapper
 *
 * @author 山野羡民
 */
@Mapper
public interface CryptocurrencyIncreasingTaskRecordMapper extends CoreMapper<CryptocurrencyIncreasingTaskRecordDO> {

    default void submitTodayTask(Long userId, Long taskId) {
        CryptocurrencyIncreasingTaskRecordDO aDo = getOrCreateToday(userId, taskId);
        aDo.setCount(aDo.getCount() + 1);
        updateById(aDo);
    }

    default void completeTodayTask(Long userId, Long taskId) {
        CryptocurrencyIncreasingTaskRecordDO aDo = getOrCreateToday(userId, taskId);
        aDo.setCompleted(true);
        updateById(aDo);
    }

    @Nonnull
    default CryptocurrencyIncreasingTaskRecordDO getOrCreateToday(Long userId, Long taskId) {
        return getOrCreate(userId, taskId, LocalDate.now());
    }

    @Nonnull
    default CryptocurrencyIncreasingTaskRecordDO getOrCreate(Long userId, Long taskId, LocalDate date) {
        CoreLambdaQueryWrapper<CryptocurrencyIncreasingTaskRecordDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(CryptocurrencyIncreasingTaskRecordDO::getUserId, userId)
                .eq(CryptocurrencyIncreasingTaskRecordDO::getTaskId, taskId)
                .eq(CryptocurrencyIncreasingTaskRecordDO::getDate, date);
        CryptocurrencyIncreasingTaskRecordDO aDo = selectOne(wrapper);
        if (aDo == null) {
            aDo = new CryptocurrencyIncreasingTaskRecordDO();
            aDo.setTaskId(taskId);
            aDo.setUserId(userId);
            aDo.setDate(date);
            aDo.setCount(0);
            aDo.setReturned(false);
            aDo.setCompleted(false);
            insert(aDo);
        }
        return aDo;
    }

    default Integer countCompleteDays(Long userId, Long taskId) {
        CoreLambdaQueryWrapper<CryptocurrencyIncreasingTaskRecordDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(CryptocurrencyIncreasingTaskRecordDO::getUserId, userId)
                .eq(CryptocurrencyIncreasingTaskRecordDO::getTaskId, taskId)
                .eq(CryptocurrencyIncreasingTaskRecordDO::getCompleted, true);
        Long count = selectCount(wrapper);
        if (count == null) {
            return 0;
        }
        return count.intValue();
    }


    default List<CryptocurrencyIncreasingTaskRecordDO> selectListByNeedReturned(LocalDate date) {
        CoreLambdaQueryWrapper<CryptocurrencyIncreasingTaskRecordDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(CryptocurrencyIncreasingTaskRecordDO::getDate, date);
        wrapper.eq(CryptocurrencyIncreasingTaskRecordDO::getReturned, false);
        return selectList(wrapper);
    }

}